﻿using Coding4Fun.Lockscreen.Web.DataStore;
using Coding4Fun.Lockscreen.Web.Models;
using System.IO;
using System.Web;
using System.Web.Mvc;

namespace Coding4Fun.Lockscreen.Web.Controllers
{
    public class HomeController : Controller
    {
        DataEngine engine;

        public HomeController()
        {
            engine = new DataEngine();
        }

        public ActionResult MainView()
        {
            var categories = engine.GetAllCategories();
            
            return View(categories);
        }

        public ActionResult Images(int categoryId)
        {
            var images = engine.GetImagesByCategoryId(categoryId);

            if (images != null)
            {
                ViewData["CID"] = categoryId;
                return View(images);
            }

            return View("MainView");
        }

        public ActionResult AddCategory()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddCategory(Category category)
        {
            if (ModelState.IsValid)
            {
                engine.AddCategory(category);
                return RedirectToAction("MainView");
            }

            return View();
        }

        public ActionResult EditCategory(int categoryId)
        {
            Category category;

            category = engine.GetCategoryById(categoryId);

            if (category != null)
                return View(category);

            return View("MainView");
        }

        [HttpPost]
        public ActionResult EditCategory(Category category)
        {
            if (ModelState.IsValid)
            {
                engine.UpdateCategory(category);
                return RedirectToAction("MainView");
            }

            return View();
        }

        public ActionResult DeleteCategory(int categoryId)
        {
            engine.DeleteCategoryFromId(categoryId);

            return RedirectToAction("MainView");
        }

        public ActionResult EditImage(int imageId)
        {
            Image image = engine.GetImageById(imageId);

            if (image != null)
                return View(image);

            return View("MainView");
        }

        [HttpPost]
        public ActionResult EditImage(Image image)
        {
            if (ModelState.IsValid)
            {
                engine.UpdateImage(image);
                return RedirectToAction("Images", new { categoryId = image.CategoryID });
            }

            return View();
        }

        public ActionResult AddImage(int categoryId)
        {
            Image image = new Image();
            image.CategoryID = categoryId;

            return View(image);
        }

        [HttpPost]
        public ActionResult AddImage(HttpPostedFileBase file, Image image)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Uploads"), image.CategoryID.ToString(), fileName);

                string dirPath = Path.GetDirectoryName(path);
                if (!Directory.Exists(dirPath))
                    Directory.CreateDirectory(dirPath);

                file.SaveAs(path);
                string applicationUrl = string.Format("{0}://{1}{2}",
                    HttpContext.Request.Url.Scheme,
                    HttpContext.Request.ServerVariables["HTTP_HOST"],
                    (HttpContext.Request.ApplicationPath.Equals("/")) ? string.Empty : HttpContext.Request.ApplicationPath
                    );

                image.URL = Path.Combine(applicationUrl, "Uploads", image.CategoryID.ToString(), fileName);
            }

            if (ModelState.IsValid && image.URL != null)
            {
                engine.AddImage(image);
                return RedirectToAction("Images", new { categoryID = image.CategoryID });
            }

            return View();
        }

        public ActionResult DeleteImage(int imageId, int categoryId)
        {
            engine.DeleteImageFromId(imageId);

            return RedirectToAction("Images", new { categoryId = categoryId });
        }

    }
}
